﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoPartsSearch.Domain.Abstract;

namespace AutoPartsSearch.Web.Infrastructure.Cache
{
    public class CacheExecutor
    {
        public static T Execute<T>(Func<T> function, int timeInMinutesToHoldCache, params object[] keyParameters)
        {
            var key = CacheHelper.CreateKey(keyParameters);
            var result = CacheHelper.GetFromCache(key);
            if (result == null)
            {
                var funcResult = function();
                CacheHelper.AddToCache(funcResult, key, timeInMinutesToHoldCache);
                return funcResult;
            }
            return (T)result;
        }
    }
}